package com.example.enrollservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.enrollservice.entity.enrollment;
import com.example.enrollservice.mapper.enrollmentMapper;
import com.example.enrollservice.service.enrollmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class emImpl extends ServiceImpl<enrollmentMapper, enrollment> implements enrollmentService {
    
    @Autowired
    private enrollmentMapper enrollmentMapper;
    
    @Override
    public List<Map<String, Object>> getStudentEnrollments(int userId) {
        return enrollmentMapper.getStudentEnrollments(userId);
    }
    
    @Override
    public List<Map<String, Object>> getCourseEnrollments(int courseId) {
        return enrollmentMapper.getCourseEnrollments(courseId);
    }
    
    @Override
    public String approveEnrollment(Map<String, Object> requestBody) {
        try {
            int userId = (int) requestBody.get("userId");
            int courseId = (int) requestBody.get("courseId");
            
            enrollment enroll = enrollmentMapper.getEnrollmentByUserAndCourse(userId, courseId);
            if (enroll == null) {
                return "选课记录不存在";
            }
            
            // 使用LambdaUpdateWrapper精确更新指定字段
            LambdaUpdateWrapper<enrollment> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(enrollment::getId, enroll.getId())
                    .set(enrollment::getStatus, 1) // 1 = 已批准
                    .set(enrollment::getDecide_time, java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                    .set(enrollment::getDecided_by, (int) requestBody.get("decidedBy"))
                    .set(enrollment::getRemark, (String) requestBody.get("remark"));
            
            update(updateWrapper);
            return "ok";
        } catch (Exception e) {
            return "审批失败: " + e.getMessage();
        }
    }
    
    @Override
    public String rejectEnrollment(Map<String, Object> requestBody) {
        try {
            int userId = (int) requestBody.get("userId");
            int courseId = (int) requestBody.get("courseId");
            
            enrollment enroll = enrollmentMapper.getEnrollmentByUserAndCourse(userId, courseId);
            if (enroll == null) {
                return "选课记录不存在";
            }
            
            // 使用LambdaUpdateWrapper精确更新指定字段
            LambdaUpdateWrapper<enrollment> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(enrollment::getId, enroll.getId())
                    .set(enrollment::getStatus, 2) // 2 = 已拒绝
                    .set(enrollment::getDecide_time, java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                    .set(enrollment::getDecided_by, (int) requestBody.get("decidedBy"))
                    .set(enrollment::getRemark, (String) requestBody.get("remark"));
            
            update(updateWrapper);
            return "ok";
        } catch (Exception e) {
            return "拒绝失败: " + e.getMessage();
        }
    }
    
    @Override
    public enrollment getEnrollmentByUserAndCourse(int userId, int courseId) {
        return enrollmentMapper.getEnrollmentByUserAndCourse(userId, courseId);
    }
}
